<?php
include_once ('../../php/postgre.php');
$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];
if ($act == 'simpan_skpd') {
    $idskpd = $_POST['id_skpd'];
    $nmskpd = $_POST['nama_skpd'];
    $kepala = $_POST['nama_kepala'];
    $nip = $_POST['nip'];
    $leveluser = $_POST['user_level'];

    if (!is_numeric($idskpd)) {
        echo "Err : invalid id.";
        exit;
    }

    if ($idskpd > 0) {
        exec_query("update mus_skpd set id_skpd=" . $idskpd . ", nama_skpd='" . $nmskpd . "', nama_kepala_skpd='" . $kepala . "', nip='" . $nip . "', id_uslevel=" . $leveluser . " where id_skpd=" . $idskpd);
        echo 'success__';
		logs('Mengubah master user RPJM','SKPD : '.$nmskpd);
    } else {
        $maxid = get_maxid('id_skpd', 'mus_skpd');
        exec_query("insert into mus_skpd (id_skpd, nama_skpd, nama_kepala_skpd, nip, id_uslevel) values(" . $maxid . ",'" . $nmskpd . "', '" . $kepala . "', '" . $nip . "', " . $leveluser . ")");
        $st = get_data('select id_skpd from mus_skpd where id_skpd=' . $maxid);
        if ($st['id_skpd'] == $maxid) {
            echo 'success__';
            $idskpd = $maxid;
			logs('Menambah master user RPJM','SKPD : '.$nmskpd);
        }
    }

    echo $idskpd . '__';
    view_skpd();
    
} else if ($act == 'hapus_skpd') {
    $idskpd = $_POST['id_skpd'];
    if (is_numeric($idskpd)) {
		$rpjm = get_data('select nama_skpd from mus_skpd where id_skpd='.$idskpd);
        exec_query("delete from mus_skpd where id_skpd=" . $idskpd);
        echo 'success__';
		logs('Menghapus master user RPJM','SKPD '.$rpjm['nama_skpd']);
    } else {
        echo 'error';
    }
    view_skpd();
    
} else if ($act == 'ubah_skpd') {
    $idskpd = $_POST['id_skpd'];
    if (is_numeric($idskpd)) {
        $data = get_data("select id_skpd,  nama_skpd, nama_kepala_skpd, nip, id_uslevel from mus_skpd where id_skpd=".$idskpd);
        $ress = implode($data, '__');
        print_r($ress);
        echo "__";
    } else {
        echo 'error';
    }
    
    view_skpd();
}

function view_skpd() {
    
                    $x=1;
                    $skpd = get_datas("select id_skpd, kode_skpd, nama_skpd, nama_kepala_skpd, nip, username, password, uslevel from mus_skpd s, user_level u where s.id_uslevel=u.id_uslevel order by nama_skpd  ");
                    foreach ($skpd as $skpd) {
                        ?><tr>
                            <td><?php echo $x?></td>
                            <td><?php echo $skpd['kode_skpd'] ?></td>
                            <td><?php echo $skpd['nama_skpd'] ?></td>
                            <td><?php echo $skpd['nama_kepala_skpd'] ?></td>
                            <td><?php echo $skpd['nip'] ?></td>
                            <td><?php echo $skpd['username'] ?></td>
                            <td><?php echo $skpd['password'] ?></td>
                            <td><?php echo $skpd['uslevel'] ?></td>
                            <td>
                                <a class="btn btn-danger bt-hapus" name="<?php echo $skpd['id_skpd']; ?>"><i class="icon-trash icon-white"></i></a>
                                <a class="btn btn-info bt-edit" name="<?php echo $skpd['id_skpd']; ?>"><i class="icon-edit icon-white"></i></a>
                            </td>
                        </tr>
                        <?php
                        $x++;
                    }
                  
}
?>

